<script lang="ts">
	import { menus, isSideBarOpen } from '$store/menus';
	import { variables } from '$store/g';
	const openSidebar = () => {
		isSideBarOpen.set(true);
	};
</script>

<header class="mx-auto flex  h-16 justify-between px-0 md:sticky md:flex-row">
	<div class="flex flex-grow-0 items-center w-10 pl-1 md:w-40 md:h-16">
		<a class="md:flex md:items-center md:justify-end" href="https://www.biling.club">
			<svg
				class="md:flex-grow-0 w-16 h-12 object-contain fill-current text-primary"
				viewBox="0 0 107 128"
				><title>svelte-logo</title><path
					d="M94.1566,22.8189c-10.4-14.8851-30.94-19.2971-45.7914-9.8348L22.2825,29.6078A29.9234,29.9234,0,0,0,8.7639,49.6506a31.5136,31.5136,0,0,0,3.1076,20.2318A30.0061,30.0061,0,0,0,7.3953,81.0653a31.8886,31.8886,0,0,0,5.4473,24.1157c10.4022,14.8865,30.9423,19.2966,45.7914,9.8348L84.7167,98.3921A29.9177,29.9177,0,0,0,98.2353,78.3493,31.5263,31.5263,0,0,0,95.13,58.117a30,30,0,0,0,4.4743-11.1824,31.88,31.88,0,0,0-5.4473-24.1157"
				/><path
					d="M45.8171,106.5815A20.7182,20.7182,0,0,1,23.58,98.3389a19.1739,19.1739,0,0,1-3.2766-14.5025,18.1886,18.1886,0,0,1,.6233-2.4357l.4912-1.4978,1.3363.9815a33.6443,33.6443,0,0,0,10.203,5.0978l.9694.2941-.0893.9675a5.8474,5.8474,0,0,0,1.052,3.8781,6.2389,6.2389,0,0,0,6.6952,2.485,5.7449,5.7449,0,0,0,1.6021-.7041L69.27,76.281a5.4306,5.4306,0,0,0,2.4506-3.631,5.7948,5.7948,0,0,0-.9875-4.3712,6.2436,6.2436,0,0,0-6.6978-2.4864,5.7427,5.7427,0,0,0-1.6.7036l-9.9532,6.3449a19.0329,19.0329,0,0,1-5.2965,2.3259,20.7181,20.7181,0,0,1-22.2368-8.2427,19.1725,19.1725,0,0,1-3.2766-14.5024,17.9885,17.9885,0,0,1,8.13-12.0513L55.8833,23.7472a19.0038,19.0038,0,0,1,5.3-2.3287A20.7182,20.7182,0,0,1,83.42,29.6611a19.1739,19.1739,0,0,1,3.2766,14.5025,18.4,18.4,0,0,1-.6233,2.4357l-.4912,1.4978-1.3356-.98a33.6175,33.6175,0,0,0-10.2037-5.1l-.9694-.2942.0893-.9675a5.8588,5.8588,0,0,0-1.052-3.878,6.2389,6.2389,0,0,0-6.6952-2.485,5.7449,5.7449,0,0,0-1.6021.7041L37.73,51.719a5.4218,5.4218,0,0,0-2.4487,3.63,5.7862,5.7862,0,0,0,.9856,4.3717,6.2437,6.2437,0,0,0,6.6978,2.4864,5.7652,5.7652,0,0,0,1.602-.7041l9.9519-6.3425a18.978,18.978,0,0,1,5.2959-2.3278,20.7181,20.7181,0,0,1,22.2368,8.2427,19.1725,19.1725,0,0,1,3.2766,14.5024,17.9977,17.9977,0,0,1-8.13,12.0532L51.1167,104.2528a19.0038,19.0038,0,0,1-5.3,2.3287"
					class="text-white fill-current"
				/></svg
			>
			<span class="hidden md:flex-grow md:block md:text-base">{$variables.siteName}</span>
		</a>
	</div>
	<nav class="flex flex-grow justify-end">
		<ul class="hidden md:flex relative items-center justify-start h-full list-none p-0 ml-0">
			{#each $menus as item}
				<li
					class="relative flex justify-center w-32 h-full {item.subItems && item.subItems.length > 0
						? 'dropdown'
						: ''} {item.active ? 'selected' : 'unselected'}"
				>
					{#if item.subItems && item.subItems.length > 0}
						<span class="flex cursor-pointer items-center h-full text-sm">{item.title}</span>
						<ul
							class="dropdown-menu"
						>
							{#each item.subItems as subItem}
								<li class="flex justify-start w-32 h-8">
									<a class="flex items-center h-full text-sm" href={subItem.href}>{subItem.title}</a
									>
								</li>
							{/each}
						</ul>
					{:else}
						<a class="flex items-center h-full text-sm" href={item.href}>{item.title}</a>
					{/if}
				</li>
			{/each}
		</ul>
		<div class="md:hidden flex-grow self-center text-center text-xl">促销小助手</div>
	</nav>
	<div class="flex-grow-0 w-40 h-full flex justify-center items-center">
		<a
			class="hidden md:rounded-3xl md:px-4 py-2 md:inline-block md:bg-primary md:text-white"
			href="void:(0)">免费体验</a
		>
		<button
			href="#"
			class="md:hidden iconfont icon-menu w-full h-full text-lg"
			on:click={openSidebar}
		/>
	</div>
</header>

<style lang="scss">
	li::before {
		@apply w-full h-0 absolute bottom-0 left-0 border-none;
		content: '';
	}
	.selected::before {
		@apply border-b-4 border-solid border-primary;
	}
	.unselected:hover::before {
		@apply border-b-4 border-solid border-primary;
	}
	.dropdown {
		.dropdown-menu {
			@apply top-12 pl-4 py-4 hidden absolute bg-white border-solid border border-gray-100 w-32 rounded-lg shadow-2xl;
		}
		&:hover {
			.dropdown-menu {
				display: block;
			}
		}
	}
</style>
